package com.fanqwz.hexieshe.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.fanqwz.hexieshe.BR;
import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.databinding.ArticlePriefListItemBinding;
import com.fanqwz.hexieshe.domain.HeadLine;
import com.fanqwz.hexieshe.model.HeadLineModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fanqw on 2017/7/1.
 */

public class HeadLineAdapter extends RecyclerView.Adapter<HeadLineAdapter.ViewHolder> {
    private List<HeadLineModel> headlines = new ArrayList<>(0);
    private View.OnClickListener onClickListener;
    @Inject
    Context context;

    @Inject
    public HeadLineAdapter() {
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public List<HeadLineModel> getHeadlines() {
        return headlines;
    }

    public void setHeadlines(List<HeadLineModel> headlines) {
        this.headlines = headlines;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ArticlePriefListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.article_prief_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(binding.getRoot());
        viewHolder.setBinding(binding);
        binding.getRoot().setOnClickListener(onClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.getBinding().setVariable(BR.headline, headlines.get(position));
        holder.getBinding().executePendingBindings();

        if (headlines.get(position).getImageUrl() != null) {
            holder.imageView.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(headlines.get(position).getImageUrl())
                    .into(holder.imageView);

            if (position < getItemCount() - 2)
                Glide.with(context)
                        .load(headlines.get(position + 1).getImageUrl())
                        .preload();

            if (position > 1)
                Glide.with(context)
                        .load(headlines.get(position - 1).getImageUrl())
                        .preload();
        } else {
            holder.imageView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return headlines.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.imageview) ImageView imageView;

        public void setBinding(ArticlePriefListItemBinding binding) {
            this.binding = binding;
        }

        public ArticlePriefListItemBinding getBinding() {
            return binding;
        }

        private ArticlePriefListItemBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnHeadLineClickedListener {
        void OnHeadLineClicked(HeadLine headLine);
    }
}
